package main

import (
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
)

func main() {
	fmt.Println(GetInternetIp())
	fmt.Println(GetIntranetIp())
}

// 外网ip
func GetInternetIp() string {
	resp, err := http.Get("http://myip.ipip.net")
	if err != nil {
		return ""
	}
	defer resp.Body.Close()
	c, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	return string(c)
}

// 内网ip
func GetIntranetIp() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return ""
	}
	ips := make([]string, 0)
	for _, address := range addrs {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ips = append(ips, ipnet.IP.String())
			}
		}
	}
	return strings.Join(ips, ", ")
}
